package git_cmd

import (
	"context"
	"fmt"
	"os/exec"
	"time"

	"atomgit.com/openlinksaas/gitwrap/utils"
	"github.com/spf13/cobra"
)

var _removeBranchForce = false

var _RemoveBranchCmd = &cobra.Command{
	Use: "removeBranch",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) != 1 {
			utils.OutputError(fmt.Errorf("need param [branch]"))
			return
		}

		branch := args[0]

		ctx, cancel := context.WithTimeout(cmd.Context(), 30*time.Second)
		defer cancel()

		cmdPath, err := exec.LookPath("git")
		if err != nil {
			utils.OutputError(err)
			return
		}

		removeFlag := "-d"
		if _removeBranchForce {
			removeFlag = "-D"
		}

		gitCmd := exec.CommandContext(ctx, cmdPath, "branch", removeFlag, branch)
		gitCmd.Dir = gitFlagLocalPath
		_, err = gitCmd.Output()
		if err != nil {
			utils.OutputError(err)
			return
		}
		utils.OutputData("")
	},
}

func init() {
	_RemoveBranchCmd.Flags().BoolVar(&_removeBranchForce, "force", false, "force remove branch")
}
